const express = require('express');
const userRouter = express.Router();
const userSer = require('../../services/userService');
const tokenSer = require('../../util/token')

// 注册
userRouter.post('/register', async(req, res) => {

        let { username, password } = req.body;
        console.log(username, password);
        if (username == undefined || password == undefined) return res.send({
                data: {},
                meta: {
                    msg: '参数错误',
                    status: 400
                }
            })
            // 查询是否存在该用户
        let result = await userSer.findUser(username);
        if (result.res1 != false) return res.send({
            data: {},
            meta: {
                msg: '用户名重复',
                status: 400
            }
        });
        console.log('是否存在用户',result)
        // 添加用户
        let result2 = await userSer.addUser(req.body);
        if (!result2) return res.send({
            data: {},
            meta: {
                msg: '注册失败',
                status: 500
            }
        })
        res.send({
            data: {
                username: result2.username,
                useremail: result2.useremail,
                usermobile: result2.usermobile,
            },
            meta: {
                msg: '注册成功',
                status: 201,
            }
        })
    })
    //登录
userRouter.post('/login', async(req, res) => {
        let { username, password } = req.body;
        if (username == undefined || password == undefined) return res.send({
            data: {},
            meta: {
                msg: '参数错误',
                status: 400
            }
        })
        let result = await userSer.findUser(username, password);
        if (result.res1 == false) return res.send({
            data: {},
            meta: {
                msg: '用户名不存在',
                status: 400
            }
        })
        if (result.res2 == false) return res.send({
            data: {},
            meta: {
                msg: '密码错误',
                status: 400
            }
        })
        let token = tokenSer.on(username);
        res.send({
            data: {
                token: token,
                username: result.res2.username,
                useremail: result.res2.useremail,
                usermobile: result.res2.usermobile,
            },
            meta: {
                msg: '登录成功',
                status: 200
            }
        })
    })
    // 修改密码
userRouter.put('/edit', async(req, res) => {
    let { username, password } = req.body;
    if (username == undefined || password == undefined) return res.send({
        data: {},
        meta: {
            msg: '参数错误',
            status: 400
        }
    })
    let result = await userSer.findUser(username, password);
    // 判断密码是否与上次重复
    if (result.res1 == false) return res.send({
        data: {},
        meta: {
            msg: '用户名错误',
            status: 400
        }
    })
    if (result.res2 != false) return res.send({
        data: {},
        meta: {
            msg: '与上次密码相同',
            status: 400
        }
    })
    let result2 = await userSer.updateUser(username, req.body);
    if (result2 == false) return res.send({
        data: {},
        meta: {
            msg: '修改失败',
            status: 400
        }
    })
    res.send({
        data: {
            username: result2.username,
            useremail: result2.useremail,
            usermobile: result2.usermobile,
        },
        meta: {
            msg: '修改成功',
            status: 201,
        }
    })
})

//反馈
userRouter.post('/feedback',async (req,res)=>{
    
})


module.exports = userRouter